package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.TransferLine;
import com.zyd.shiro.business.service.PakTransferLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakTransferLine;
import com.zyd.shiro.persistence.mapper.PakTransferLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakTransferLineServiceImpl implements PakTransferLineService {

    @Autowired
    private PakTransferLineMapper pakTransferLineMapper;

    @Override
    public TransferLine insert(TransferLine transferLine) {
        Assert.notNull(transferLine, "对象不可为空！");
        transferLine.getPakTransferLine().setUpdateTime(new Date());
        transferLine.getPakTransferLine().setCreateTime(new Date());
        pakTransferLineMapper.insertSelective(transferLine.getPakTransferLine());
        return transferLine;
    }

    @Override
    public void insertList(List<TransferLine> transferLines) {
            for(TransferLine transferLine:transferLines){
               pakTransferLineMapper.insertSelective(transferLine.getPakTransferLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakTransferLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(TransferLine transferLine) {
        Assert.notNull(transferLine, "对象不可为空！");
        transferLine.setUpdateTime(new Date());
        return pakTransferLineMapper.updateByPrimaryKey(transferLine.getPakTransferLine()) > 0;
    }

    @Override
    public boolean updateSelective(TransferLine transferLine) {
        Assert.notNull(transferLine, "对象不可为空！");
        transferLine.setUpdateTime(new Date());
        return pakTransferLineMapper.updateByPrimaryKeySelective(transferLine.getPakTransferLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public TransferLine getByPrimaryKey(Long primaryKey) {
        PakTransferLine pakTransferLine = pakTransferLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakTransferLine ? null : new TransferLine(pakTransferLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public TransferLine getOneByEntity(TransferLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakTransferLine pakTransferLine = pakTransferLineMapper.selectOne(test.getPakTransferLine());
        return null == pakTransferLine ? null : new TransferLine(pakTransferLine);
    }

    @Override
    public List<TransferLine> listAll() {
        List<PakTransferLine> pakTransferLines = pakTransferLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakTransferLines)) {
            return null;
        }
        List<TransferLine> transferLines = new ArrayList<>();
        for (PakTransferLine pakTransferLine : pakTransferLines) {
            transferLines.add(new TransferLine(pakTransferLine));
        }
        return transferLines;
    }

    @Override
    public List<TransferLine> listByEntity(TransferLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<TransferLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakTransferLine> pakTransferLines = pakTransferLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakTransferLines)) {
            return null;
        }
        List<TransferLine> transferLines = new ArrayList<>();
        for (PakTransferLine pakTransferLine : pakTransferLines) {
            transferLines.add(new TransferLine(pakTransferLine));
        }
        PageInfo bean = new PageInfo<PakTransferLine>(pakTransferLines);
        bean.setList(transferLines);
        return bean;
    }
@Override
    public void increase(PakTransferLine pakTransferLine) {
        pakTransferLineMapper.insert(pakTransferLine);
    }

    @Override
    public List<PakTransferLine> findByTransfer(Long id) {
        return pakTransferLineMapper.findByTransfer(id);
    }


}
